﻿#ifndef ___PCLIB_S_BOX___
#define ___PCLIB_S_BOX___


#include "../m_core/m_core.hpp"


/**
*@brief        CPCBoxCodec用到的对应表，生成的算法代码如下：
*            //生成顺序box
*            #define BYTECNT 256
*            unsigned char ordered[BYTECNT];
*            for(int i = 0 ;i < BYTECNT ;i++)
*            {
*                ordered[i] = i;
*            }
*            //打乱顺序box来生成加密box
*            std::string suffle = CPCRandom::PCShuffle(std::string((char*)ordered,BYTECNT));
*            //根据加密box生成解密box
*            unsigned char decbox[BYTECNT];
*            for(int i = 0 ; i < BYTECNT; i++)
*            {
*                for(int j = 0 ; j < BYTECNT; j++)
*                {
*                    if(i == (unsigned char)suffle[j])
*                    {
*                        decbox[i] = j;
*                        break;
*                    }
*                }
*            }
*            //将加密box和解密box生成代码字符串
*            std::string encboxcode = "static const unsigned char PCBOXCODEC_E[256] = {0x";
*            encboxcode = encboxcode + CPCStrUtil::PCBytes2Dsp((unsigned char*)suffle.data(), BYTECNT, ",0x", true);
*            encboxcode = encboxcode + "};";
*            std::string decboxcode  = "static const unsigned char PCBOXCODEC_D[256] = {0x";
*            decboxcode = decboxcode + CPCStrUtil::PCBytes2Dsp((unsigned char*)decbox, BYTECNT, ",0x", true);
*            decboxcode = decboxcode + "};";
*/
static const unsigned char PCBOXCODEC_E[256] = { 0xcd, 0xf0, 0xa2, 0xe2, 0xed, 0x88, 0xe3, 0x98, 0xbc, 0x66, 0xc1, 0x7c, 0xf4, 0xdb, 0x47, 0x96, 0xf6, 0x6c, 0x5e, 0x11, 0xaf, 0x2f, 0x40, 0x42, 0x41, 0x07, 0xde, 0x4c, 0x8a, 0x63, 0x4d, 0x51, 0xc0, 0x9b, 0x38, 0x27, 0x19, 0x03, 0x97, 0x65, 0x3d, 0x44, 0xac, 0xa7, 0x18, 0xa0, 0x61, 0x13, 0xb3, 0xb4, 0xc6, 0x21, 0x15, 0xe0, 0xc5, 0x0f, 0x78, 0xc4, 0xef, 0x2c, 0x53, 0x26, 0x2e, 0x67, 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0x9d, 0x75, 0xe1, 0xce, 0x14, 0x6a, 0x3e, 0xfc };
static const unsigned char PCBOXCODEC_D[256] = { 0x74, 0x81, 0xa4, 0x25, 0x63, 0x8a, 0x83, 0x19, 0xab, 0xb0, 0x6f, 0xf1, 0xd2, 0x70, 0xe7, 0x37, 0x65, 0x13, 0xa8, 0x2f, 0xfc, 0x34, 0xee, 0x45, 0x2c, 0x24, 0xb5, 0xe8, 0x7d, 0xbe, 0xbf, 0xa3, 0xf4, 0x33, 0x77, 0x9e, 0xf7, 0xae, 0x3d, 0x23, 0xdc, 0xb2, 0x4f, 0xec, 0x3b, 0x5a, 0x3e, 0x15, 0x80, 0x62, 0x78, 0x96, 0x51, 0x53, 0xc9, 0x6b, 0x22, 0xd5, 0x73, 0xb4, 0x75, 0x28, 0xfe, 0xb1, 0x16, 0x18, 0x17, 0x54, 0x29, 0x92, 0x46, 0x0e, 0x52, 0x8d, 0xd6, 0xc6, 0x1b, 0x1e, 0xc5, 0x94, 0x5f, 0x1f, 0xad, 0x3c, 0x40, 0x6a, 0xbc, 0x97, 0x64, 0xb8, 0x66, 0xf6, 0x9d, 0x5c, 0x12, 0x41, 0xed, 0x2e, 0xe2, 0x1d, 0x4e, 0x27, 0x09, 0x3f, 0xb3, 0x71, 0xfd, 0xa0, 0x11, 0xd1, 0xba, 0x93, 0xc3, 0xaf, 0x61, 0xf2, 0x60, 0xf9, 0x7f, 0x85, 0x38, 0xd0, 0xb9, 0xdd, 0x0b, 0xd4, 0xa2, 0x67, 0x87, 0xf0, 0xcd, 0xcc, 0xf5, 0xa6, 0x7c, 0x99, 0x05, 0x8c, 0x1c, 0xcf, 0xc4, 0x79, 0xbb, 0xca, 0x58, 0xb7, 0x59, 0x5d, 0x49, 0x47, 0x0f, 0x26, 0x07, 0x5e, 0xc1, 0x21, 0x7b, 0xf8, 0x9b, 0xac, 0x2d, 0xd3, 0x02, 0x48, 0xbd, 0xf3, 0x89, 0x2b, 0x9a, 0xc0, 0x44, 0xd9, 0x2a, 0xe1, 0xaa, 0x14, 0x7e, 0x72, 0x6c, 0x30, 0x31, 0x5b, 0x9f, 0xe0, 0x95, 0xe3, 0x4b, 0x84, 0x08, 0x4d, 0xd7, 0x50, 0x20, 0x0a, 0xef, 0xcb, 0x39, 0x36, 0x32, 0xdb, 0x43, 0xa9, 0x69, 0xe9, 0xde, 0x00, 0xfb, 0x57, 0xe4, 0xda, 0x82, 0x4c, 0x91, 0x42, 0x90, 0x55, 0x98, 0x8f, 0xea, 0x0d, 0xc7, 0x6d, 0x1a, 0xc2, 0x35, 0xfa, 0x03, 0x06, 0xce, 0x6e, 0xeb, 0xb6, 0x88, 0xa7, 0x76, 0xa1, 0x8b, 0x04, 0xc8, 0x3a, 0x01, 0xe6, 0x7a, 0x86, 0x0c, 0x56, 0x10, 0xdf, 0xe5, 0x9c, 0xd8, 0x4a, 0xff, 0x8e, 0xa5, 0x68 };

/**
*@brief        简单加密算法，加解密比base64快很多，区别是这里加密之后可能包括不可见字符
*/
class CPCBoxCodec : CPCNoCopyable
{
public:
    /**
    *@brief     加密
    *@param     input    [in]    待加密数据
    *@return    结果
    */
    static std::string BoxEncode(const std::string& input)
    {
        std::string ret;
        if (input.empty())
        {
            return ret;
        }
        ret.reserve(input.length());
        ret.clear();
        for (unsigned int i = 0; i < input.length(); i++)
        {
            ret += PCBOXCODEC_E[(unsigned char)input[i]];
        }
        return ret;
    }
    /**
    *@brief     解密
    *@param     input    [in]    待解密数据
    *@return    结果
    */
    static std::string BoxDecode(const std::string& input)
    {
        std::string ret;
        if (input.empty())
        {
            return ret;
        }
        ret.reserve(input.length());
        ret.clear();
        for (unsigned int i = 0; i < input.length(); i++)
        {
            ret += PCBOXCODEC_D[(unsigned char)input[i]];
        }
        return ret;
    }
};





#endif    //___PCLIB_S_BOX___